package com.clz.appdesign.myapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author chenliangzhi
 * @date 2018/5/22
 * @describe
 */

public class CanvasOperateView extends View {
    public CanvasOperateView(Context context) {
        super(context);
    }

    public CanvasOperateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CanvasOperateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint redPaint = generatePaint(Color.RED, Paint.Style.STROKE, 3);
        Paint greenPaint = generatePaint(Color.GREEN, Paint.Style.STROKE, 3);
        //画布平移之前
        RectF rectF = new RectF(0, 0, 400, 200);
        canvas.drawRect(rectF, redPaint);
        //画布平移之后
        canvas.translate(100, 100);
        canvas.drawRect(rectF, greenPaint);
    }

    private Paint generatePaint(int color, Paint.Style style, int width) {
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(width);
        return paint;
    }
}
